/**
 * 垃圾清理模块入口管理类，采用Singleton模式
 */

package com.tools.cleanmaster.manager;

import android.content.Context;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.controller.AppResidualScanner;
import com.tools.cleanmaster.controller.ITrashScanListener;
import com.tools.cleanmaster.controller.TrashDeepHandler;
import com.tools.cleanmaster.controller.TrashHandler;
import com.tools.cleanmaster.controller.TrashQuickHandler;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.item.AppTrashItem;
import com.tools.cleanmaster.store.GlobalConfigSharedPrefManager;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;
import com.tools.cleanmaster.utils.TrashUtils;

import java.util.List;

/**
 * @author ouyang
 */
public class TrashManager {

    private static final String TAG = "TrashManager";

    private Context mAppContext;

    private static volatile TrashManager sInstance = null;

    private TrashHandler mTrashQuickHandler;
    private TrashHandler mTrashDeepHandler;

    private TrashType[] mQuickTrashTypesDefault = new TrashType[]{
            TrashType.APK_FILE, TrashType.APP_CACHE, TrashType.APP_MEM, TrashType.UNINSTALLED_APP,
            TrashType.APP_TRASH_FILE
    };

    private TrashType[] mDeepTrashTypesDefault = new TrashType[]{
            TrashType.IMAGE_FILE, TrashType.VIDEO_FILE, TrashType.LARGE_FILE
    };

    private TrashManager(Context cxt) {
        mAppContext = cxt.getApplicationContext();
    }

    /**
     * 获取该类的唯一实例
     *
     * @return 返回该唯一实例
     */
    public static TrashManager getInstance() {
        synchronized (TrashManager.class) {
            if (sInstance == null) {
                sInstance = new TrashManager(CleanApplication.getInstance());
            }
        }
        return sInstance;
    }

    /**
     * 扫描指定类型的垃圾（异步方式）
     *
     * @param listener 扫描进度和结果监听器
     * @return 返回一个垃圾任务句柄，用于控制扫描过程（比如，取消扫描）
     */
    public TrashHandler scan(ITrashScanListener listener, boolean isDeep, boolean isBackScan) {
        // 每次会开始新扫描;
        TrashHandler handler;
        if (isDeep) {
            handler = new TrashDeepHandler(mAppContext, mDeepTrashTypesDefault);
            mTrashDeepHandler = handler;
        } else {
            handler = new TrashQuickHandler(mAppContext, mQuickTrashTypesDefault, isBackScan);
            mTrashQuickHandler = handler;
            // 非后台扫描时要记录时间
            GlobalConfigSharedPrefManager.setScanTime(CleanApplication.getInstance(), System.currentTimeMillis());
        }

        handler.scan(listener);
        return handler;
    }

    public TrashHandler scanAppTrash(ITrashScanListener listener) {
        // 每次会开始新扫描;
        TrashHandler handler;
        handler = new TrashQuickHandler(mAppContext, new TrashType[]{TrashType.APP_TRASH_FILE}, true);
        mTrashQuickHandler = handler;
        handler.scan(listener);
        return handler;
    }

    /**
     * 当应用被卸载后，可调用此接口检测其是否有残留垃圾文件(此接口应在异步线程调用)。
     *
     * @param pkgName
     * @return null will be returned if no trashes found
     */
    public List<AppTrashItem> scanUninstalledAppTrash(String pkgName) {
        return new AppResidualScanner(mAppContext, pkgName, TrashUtils.findSDPaths(mAppContext)).scan();
    }

    public void checkNotifyScan() {

        long lastScanTime = GlobalConfigSharedPrefManager.getScanTime(mAppContext);

        if (lastScanTime <= 0) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.i(TAG, "未使用过CLEANER");
            }
            return;
        }

        long currentTime = System.currentTimeMillis();
        if ((currentTime - lastScanTime) <= CleanConstant.TIME_ONE_DAY) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.i(TAG, "当前时间=" + currentTime + "与最后扫描时间=" + lastScanTime + "未超过一天");
            }
            return;
        }

        long lastNotificationTime = GlobalConfigSharedPrefManager.getScanNotificationTime(mAppContext);
        if (currentTime - lastNotificationTime <= CleanConstant.TIME_ONE_DAY) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.i(TAG, "当前时间=" + currentTime + "与最后通知栏弹出时间=" + lastNotificationTime + "未超过一天");
            }
            return;
        }

        CleanerNotificationManager.getInance(mAppContext).sendScanNotification(mAppContext, CleanConstant.PHONE_CLEANER_NOTIFICATION_TIME);
    }

    /**
     * 获取快速深度扫描Handler
     *
     * @param isDeep
     * @return
     */
    public TrashHandler getTrashHandler(boolean isDeep) {
        return isDeep ? mTrashDeepHandler : mTrashQuickHandler;
    }
}
